﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace solitaire
{
    public partial class DiscardPile : UserControl, ICardContainer
    {
        private List<Card> _cards = new List<Card>();

        public DiscardPile()
        {
            InitializeComponent();
        }

        #region ICardContainer Members

        public Card RetrieveTopCard()
        {
            Card c = _cards[_cards.Count - 1].Clone();
            _cards.RemoveAt(_cards.Count - 1);
            UpdateCardDisplay();
            return c;
        }

        public void AddCard(Card c)
        {
            _cards.Add(c);
            c.FaceUp = true;
            c.FromContainer = this;
            UpdateCardDisplay();
        }

        private void UpdateCardDisplay()
        {
            ClearAllCardsFromUI();
            double p = .2;
            int z = 1;
            foreach (Card c in _cards)
            {
                Canvas.SetZIndex(c, z++);
                Canvas.SetTop(c, p -= .2);
                Canvas.SetLeft(c, p);
                c.DragEnabled = false;
                this.LayoutRoot.Children.Add(c);
            }
            if (GameStatus.Preferences.DrawMode == DrawModes.DrawThree)
            {
                switch (_cards.Count)
                {
                    case 2:
                        Canvas.SetLeft(_cards[TotalCards - 1], 15);
                        _cards[TotalCards - 2].FaceUp = true;
                        break;
                    case 1:
                    case 0:
                        break;
                    default:
                        Canvas.SetLeft(_cards[TotalCards - 1], 30);
                        Canvas.SetLeft(_cards[TotalCards - 2], 15);
                        _cards[TotalCards - 2].FaceUp = true;
                        _cards[TotalCards - 3].FaceUp = true;
                        break;
                }
            }
            if (TotalCards > 0) _cards[TotalCards - 1].DragEnabled = true;
        }
        #endregion

        private void ClearAllCardsFromUI()
        {
            List<Card> killList = new List<Card>();

            foreach (UIElement eu in LayoutRoot.Children)
            {
                if (eu is Card) killList.Add((Card)eu);
            }
            while (killList.Count > 0)
            {
                LayoutRoot.Children.Remove(killList[0]);
                killList.RemoveAt(0);
            }
        }

        #region ICardContainer Members

        public void Clear()
        {
            ClearAllCardsFromUI();
            _cards.Clear();
        }

        public Card RetrieveCard(Card c)
        {
            _cards.Remove(c);
            UpdateCardDisplay();
            return c;
        }

        #endregion

        #region ICardContainer Members


        public bool IsInside(Point p)
        {
            throw new NotImplementedException();
        }


        public Card[] RetrieveCardAndDescendants(Card c)
        {
            Card cc = c.Clone();
            _cards.Remove(c);
            UpdateCardDisplay();
            return new Card[] { cc};
        }
        public int TotalCards
        {
            get { return _cards.Count; }
        }
        #endregion
    }
}
